var app = getApp();

Page({
  data: {
    order_nav: [{
      name: "全部订单"
    }, {
      name: "待处理"
    }, {
      name: "已完成"
    }, {
      name: "退款订单"
    }],
    page: 1,
    order: [],
    ac_index: 0,
    order_index: 0,
    acti_index: 0,
    sellers_id: 0,
    isLoading: false, // 是否还能上拉刷新
    isFinish: false // 是否加载完成
  },
  onLoad (query) {
    app.getUrl(this)
    app.getSystem(this)
    console.log(query)
    this.setData({
      store_info: wx.getStorageSync("store_info"),
      page: 1,
      order_index: query.index
    });
    this.getOrderList()
  },
  /**
   * @function 下拉刷新
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      isLoading: false,
      isFinish: false,
      order: []
    })
    this.getOrderList(() => {
      wx.stopPullDownRefresh()
    })
  },
  /**
   * @function 获取订单列表
   * @param {*} cb 请求成功的回调cb，因为之前封装没有promise
   */
  getOrderList (cb) {
    app.util.request({
      url: "order/list",
      cachetime: "0",
      data: {
        seller_id: this.data.store_info.seller_id,
        merchant_status: this.data.order_index, // 0.全部，1. 待处理；2已完成；3退款订单
        page: this.data.page
      },
      testapi: app.siteInfo.siteroot_admin,
      success: (res) => {
        if (this.data.order.length === 0) {
          this.setData({
            order: res.data.data.data
          })
        } else {
          this.setData({
            order: [
              ...this.data.order,
              ...res.data.data.data
            ]
          })
        }
        // 不只有1页，并且当前页码小于总页码，则表示还可以上拉加载
        if (res.data.data.last_page !== 1 && this.data.page < res.data.data.last_page ) {
          this.setData({
            isLoading: false
          })
        } else {
          this.setData({
            isFinish: true
          })
        }

        cb && cb()
      }
    });
  },
  /**
   * @function 跳转到订单详情
   * @param {*} e 
   */
  goOrderDetail (e) {
    wx.navigateTo({
      url: "./orderDetail/index?id=" + e.currentTarget.dataset.id
    });
  },
  /**
   * @function 订单筛选
   * @param {*} t 
   */
  getOrderListByStatus (e) {
    this.setData({
      order_index: e.currentTarget.dataset.index,
      page: 1,
      index: 1,
      isLoading: false,
      isFinish: false,
      order: [],
    }, this.getOrderList)
  },
  /**
   * @function 上拉加载回调
   */
  onReachBottom () {
    console.log(this.data.isLoading, this.data.page)
    if (!this.data.isLoading) {
      this.setData({
        isLoading: true,
        page: this.data.page + 1
      })
      
      this.getOrderList()
    }
  }
});